﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Media;
using FirstFloor.Documents.Fonts;

namespace FirstFloor.Documents.Pdf.Fonts
{
    /// <summary>
    /// Represents a standard Type 1 font.
    /// </summary>
    internal class StandardType1Font
        : Type1Font
    {
        private StandardType1Font(GlyphTypefaceInfo typeface, DictionaryObject fontDictionary)
            : base(fontDictionary)
        {
            this.RequestUri = typeface.Uri.ToString();
            //this.StyleSimulation = GetStyleSimulation(fontName);
        }

        /// <summary>
        /// Gets the glyph index for specified glyph name.
        /// </summary>
        /// <param name="glyphName">Name of the glyph.</param>
        /// <returns></returns>
        protected override ushort? GetGlyphIndex(string glyphName)
        {
            if (glyphName != null) {

            }

            return base.GetGlyphIndex(glyphName);
        }

        /// <summary>
        /// Creates the specified Standard Type 1 font.
        /// </summary>
        /// <param name="fontName">Name of the font.</param>
        /// <param name="fontDictionary">The font dictionary.</param>
        /// <returns></returns>
        public static FontBase CreateStandardType1Font(string fontName, DictionaryObject fontDictionary)
        {
            var typeface = StandardFonts.GetGlyphTypeface(fontName);
            return new StandardType1Font(typeface, fontDictionary);
        }

        /// <summary>
        /// Creates a substitute font for specified font dictionary.
        /// </summary>
        /// <param name="fontDictionary">The font dictionary.</param>
        /// <returns></returns>
        public static FontBase CreateSubstituteFont(DictionaryObject fontDictionary)
        {
            var fontDescriptor = fontDictionary.Get<DictionaryObject>("FontDescriptor");
            var fontName = fontDescriptor.Get<string>("FontName");
            var flags = (FontFlags)fontDescriptor.Get<int>("Flags");
            var standardName = "Helvetica";

            if ((flags & FontFlags.FixedPitch) > 0) {
                standardName = "Courier";
            }
            else if ((flags & FontFlags.Serif) > 0) {
                standardName = "TimesNewRoman";
            }
            
            // try to determine font style
            if (fontName.EndsWith("Bold") || fontName.EndsWith("Semibold")) {
                standardName += "-Bold";
            }

            return CreateStandardType1Font(standardName, fontDictionary);
        }
    }
}
